﻿/*
 *  PassBag is multi-platform software that securely manage/store passwords
 *  Copyright (C) <2009>  <Hugo Rodrigues PEREIRA>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *	
 *  You can find a copy of the GPLv3 at project <root directory>/gpl.txt
 *
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using PassBag.Logic.Settings;
using PassBag.Logic.Backup;
using ICSharpCode.SharpZipLib.Zip;

namespace PassBag.Presentation.Backup {
    public partial class BackupSetDialog : Form {
        protected enum BackupFileState {
            DirectoryNotExist = 1,
            OnlyDirectoryExist,
            FileExist,
        }

        protected BackupFileState fileState = BackupFileState.DirectoryNotExist;
        private string _currentPasswordFile = string.Empty;

        public string CurrentPasswordFile {
            get {
                return _currentPasswordFile;
            }
            set {                                         
                _currentPasswordFile = value;
            }
        }

        public BackupSetDialog () {
            InitializeComponent ();
            backupSetListBox.Items.Clear();


        }

        #region Load Resources

        private void LoadRessource () {
            backupSetTextBoxLabel.Text = Properties.Resources.BackupSetDialog_BackupSetTextTitle;
            backupArchiveGroupBox.Text = Properties.Resources.BackupSetDialog_GroupTitle;
            commentLabel.Text = Properties.Resources.BackupSetDialog_CommentTitle;

        }

        #endregion

        protected void LoadBackupSetItems () {
            string backupSetPath = string.Empty;

            // operating system
            switch (PassBag.Utils.OSInfo.GetCurrentOS()) {
                case PassBag.Utils.OSInfo.OSInformation.UnixLinux:
                    backupSetPath = XmlSettingsManager.Settings.BackupDefaultLinux;
                    break;

                case PassBag.Utils.OSInfo.OSInformation.MacOSX:
                    backupSetPath = XmlSettingsManager.Settings.BackupDefaultOSX;
                    break;

                case PassBag.Utils.OSInfo.OSInformation.Windows:
                default:
                    backupSetPath = XmlSettingsManager.Settings.BackupDefaultWindows;
                    break;
            }

            UpdateFileState (backupSetPath);

            if (fileState != BackupFileState.DirectoryNotExist) {
                backupSetTextBox.Text = backupSetPath;
                UpdateBackupSetItems ();
            }
        }

        protected void UpdateFileState (string backupSetPath) {
            if (!string.IsNullOrEmpty (backupSetPath)) {
                if (System.IO.File.Exists (backupSetPath)) {
                    fileState = BackupFileState.FileExist;
                }
                else if (System.IO.Directory.Exists (BackupSet.GetFilePath (backupSetPath))) {
                    fileState = BackupFileState.OnlyDirectoryExist;
                }
            }
        }
                                     
        protected void UpdateBackupSetItems () {
            backupSetListBox.Items.Clear ();

            if (fileState == BackupFileState.FileExist) {
                try {
                    List<BackupZipEntry> backupFileList = BackupSet.GetBackupFleList (backupSetTextBox.Text);

                    foreach (BackupZipEntry zipFile in backupFileList) {
                        backupSetListBox.Items.Add (zipFile);
                    }
                }
                catch (ZipException ex) {

                    if (ex.Message.Equals (BackupSet.InvalidBackupFileException)) {
                        MessageDialog.ShowError (this, Properties.Resources.BackupSetDialog_MsgInvalideBackupSet);
                    }
                }
            }
        }
                                                                              
        private void backupDefaultWindowsButton_Click (object sender, EventArgs e) {
            object file = BackupSetFileDialog.SelectFile (Properties.Resources.BackupSetDialog_SelectBackupSetTitle);

            if (!file.Equals (DialogResult.Cancel)) {
                UpdateFileState (file.ToString ());

                UpdateBackupSetItems ();
                backupSetTextBox.Text = file.ToString ();
            }
        }

        private void backupSetListBox_SelectedIndexChanged (object sender, EventArgs e) {
            if (backupSetListBox.SelectedIndex != -1) { // if and item is selected
                backupSetCommentTextBox.Text = ((BackupZipEntry)backupSetListBox.SelectedItem).Comment;
            }
            else {
                backupSetCommentTextBox.Clear ();
            }
        }
    }
}
